﻿/**
 * 24 April 2009                                               Alexey Kolonitsky
 * 19 June  2009                                               Alexey Kolonitsky
 * TYT.BY
 * 
 * Simple video component for video banners 
 */

package org.kolonitsky.alexey.media
{
    import org.kolonitsky.alexey.banner.components.*;
	import flash.display.MovieClip;
	import flash.events.AsyncErrorEvent;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;

    /**
     * Simple video stream class. This class provide pretty simple interface
     * for video playback.
     */
	dynamic public class VideoStream extends MovieClip
	{

        public static const DEFAULT_URL: String = "assets/video.flv";

        public static const DEFAULT_VOLUME: Number = 0.5;
    
        public var status: String = VideoStreamStatus.STOP;
		
		private var url: String = DEFAULT_URL;

		private var con: NetConnection;

		private var str: NetStream;

		private var vid: Video;

		private var isStart: Boolean = false;
		private var isPlay: Boolean = false;
		private var isStop: Boolean = true;
		
		
		public function VideoStream(url:String=DEFAULT_URL, w:Number=240, h:Number=180)
		{
            trace("INF: VideoStream: url=" + url + ", size=" + w + "x" + h);
			super();

			this.url = url;

			con = new NetConnection();
			con.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			con.addEventListener(IOErrorEvent.IO_ERROR, onError);
			con.connect(null);

			str = new NetStream(con);
			str.client = {onMetaData:onMetaData};
			str.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			str.addEventListener(IOErrorEvent.IO_ERROR, onError);
			str.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
			str.soundTransform = new SoundTransform(DEFAULT_VOLUME);

			vid = new Video(w, h);
			addChild(vid);
			vid.attachNetStream(str);
		}

		private function onAsyncError (event: AsyncErrorEvent): void {
			trace("VideoStream: AsyncErr: " + event.error);
		}


		/** Net status event handler **/
		private function onNetStatus (event: NetStatusEvent): void {
			trace("VideoStream: Status: " + event.info.code);
			switch (event.info.code) {
				case "NetStream.Play.Stop":
					dispatchEvent(new VideoStreamEvent(VideoStreamEvent.STOP));
					isStop = true;
					break;
			}
		}


		/** IOError Event **/
		private function onError (event: IOErrorEvent): void
        {
			trace("ERR:" + event.text);
		}


		private function onMetaData (infoObject:Object): void
        {
			trace("Metadata: ");
			for (var field:String in infoObject)
				trace("\t" + field + ": \t" + infoObject[field]);
		}


		override public function set width(value:Number):void
        {
			trace("set width: " + value);
			super.width = value;
			vid.width = value;
		}


		override public function set height(value:Number):void
        {
			trace("set height: " + value);
			super.height = value;
			vid.height = value;
		}


		/**  **/
		override public function play(): void
        {
			if (isStop) {
				trace("INF: VideoStream: play: url=" + url);
				str.play(url);
			} else {
                str.seek(0);
				str.resume();
			}
			isStart = false;
		}


		/**  **/
		override public function stop(): void
        {
			str.close();
		}


		/**  **/
		private var isMute: Boolean = false;

		public function volume (value:int): void
        {
			var v: Number = value / 100;
			str.soundTransform = new SoundTransform(v, 0);
			if (value == 0 && !isMute)
            {
				dispatchEvent(new VideoStreamEvent(VideoStreamEvent.MUTE));
				isMute = true;
			}
			if (value > 0 && isMute)
            {
				dispatchEvent(new VideoStreamEvent(VideoStreamEvent.UNMUTE));
				isMute = false;
			}
		}
	}
}